//
//  AppDelegate.m
//  Halt
//
//  Created by Chee-Kwang Beh on 14/10/12.
//  Copyright (c) 2012 Chee-Kwang Beh. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at

//  http://www.apache.org/licenses/LICENSE-2.0

//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self resetView];
}

- (NSWorkspace *)workspace;
{
	return [NSWorkspace sharedWorkspace];
}

//List arrangement
- (NSArray *)sortDescriptors;
{
    return [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"localizedName" ascending:YES]];
}

//close app when last window is closed
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
	return YES;
}

//open Google Code link
- (IBAction)openProjectLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/halt-osx/"]];
}

- (IBAction)openFAQLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:@"http://code.google.com/p/halt-osx/wiki/FAQ"]];
}

//handles the values, starts timer
- (IBAction)startTimer:(id)sender
{
    if ([secs integerValue] < 0) {
        NSAlert *alert1 = [NSAlert alertWithMessageText:@"Oops!"
                                          defaultButton:@"Okay"
                                        alternateButton:nil
                                            otherButton:nil
                              informativeTextWithFormat:@"Enter a positive number in the seconds field."];
        [alert1 runModal];
        
    } else {
        if ([secs integerValue] > 60) {
            NSAlert *alert2 = [NSAlert alertWithMessageText:@"Oops!"
                                              defaultButton:@"Okay"
                                            alternateButton:nil
                                                otherButton:nil
                                  informativeTextWithFormat:@"Enter a number lower than 61 in the seconds field."];
            [alert2 runModal];
            
        } else {
            if ([mins integerValue] < 0) {
                NSAlert *alert3 = [NSAlert alertWithMessageText:@"Oops!"
                                                  defaultButton:@"Okay"
                                                alternateButton:nil
                                                    otherButton:nil
                                      informativeTextWithFormat:@"Enter a positive number in the minutes field."];
                [alert3 runModal];
                
            } else {
                if ([mins integerValue] > 60) {
                    NSAlert *alert4 = [NSAlert alertWithMessageText:@"Oops!"
                                                      defaultButton:@"Okay"
                                                    alternateButton:nil
                                                        otherButton:nil
                                          informativeTextWithFormat:@"Enter a number lower than 61 in the minutes field."];
                    [alert4 runModal];
                    
                } else {
                    if ([hours integerValue] < 0) {
                        NSAlert *alert5 = [NSAlert alertWithMessageText:@"Oops!"
                                                          defaultButton:@"Okay"
                                                        alternateButton:nil
                                                            otherButton:nil
                                              informativeTextWithFormat:@"Enter a positive number in the hours field."];
                        [alert5 runModal];
                        
                    } else {
                        if ([hours integerValue] > 60) {
                            NSAlert *alert6 = [NSAlert alertWithMessageText:@"Oops!"
                                                              defaultButton:@"Okay"
                                                            alternateButton:nil
                                                                otherButton:nil
                                                  informativeTextWithFormat:@"Enter a number lower than 61 in the hours field."];
                            [alert6 runModal];
                            
                        } else {
                            [startTimer setEnabled:NO];
                            [stopTimer setEnabled:YES];
                            
                            [timeLeftSecs setIntegerValue:([secs integerValue])];
                            [timeLeftMins setIntegerValue:([mins integerValue])];
                            [timeLeftHours setIntegerValue:([hours integerValue])];
                            
                            //initate NSTimer
                            NSTimer *countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                                                       target:self
                                                                                     selector:@selector(timerHandler:)
                                                                                     userInfo:nil
                                                                                      repeats:YES];
                        }
                        
                    }
                }
                
            }
            
        }
        
    }
}

- (IBAction)stopTimer:(id)sender
{
    //set to do nothing
    [functionSelector selectCellWithTag:(4)];
    //invalidate timer
    [countdownTimer invalidate];
    //reset view
    [self resetView];
}

- (void)applicationKill;
{
    NSRunningApplication *selectedApp = [arrayController.selectedObjects objectAtIndex:0];
    [selectedApp terminate];
    
}

- (void)applicationHide;
{
    NSRunningApplication *selectedApp = [arrayController.selectedObjects objectAtIndex:0];
	[selectedApp hide];
}

- (void)beep;
{
    NSBeep();
}

- (void)resetView;
{
    //set time left to 00:00:00
    [timeLeftHours setStringValue:@"00"];
    [timeLeftMins setStringValue:@"00"];
    [timeLeftSecs setStringValue:@"00"];
    
    //change all textfield values to blank
    [hours setStringValue:@""];
    [mins setStringValue:@""];
    [secs setStringValue:@""];
    
    [startTimer setEnabled:YES];
    [stopTimer setEnabled:NO];
    
}


- (void)timerHandler:(NSTimer *)countdownTimer {
    if ([timeLeftHours integerValue] == 0) {
        [timeLeftHours setStringValue:@"00"]; }
    if ([timeLeftMins integerValue] == 0) {
        [timeLeftMins setStringValue:@"00"]; }
    if ([timeLeftSecs integerValue] == 0) {
        [timeLeftSecs setStringValue:@"00"]; }
    
    
    if ([timeLeftSecs integerValue] == 0) {
        if ([timeLeftMins integerValue] == 0) {
            if ([timeLeftHours integerValue] == 0) {
                [countdownTimer invalidate];
                long function = [functionSelector selectedRow];
                NSLog(@"%li", function);
                if (function == 0) {
                    [self applicationHide];
                }
                if (function == 1) {
                    [self applicationKill];
                }
                if (function == 2) {
                    [self beep];
                }
                
                [self resetView];
                [startTimer setEnabled:YES];
                
            } else {
                [timeLeftHours setIntegerValue:([timeLeftHours integerValue] - 1)];
                if ([timeLeftHours integerValue] == 9) {
                    [timeLeftHours setStringValue:@"09"];}
                if ([timeLeftHours integerValue] == 8) {
                    [timeLeftHours setStringValue:@"08"];}
                if ([timeLeftHours integerValue] == 7) {
                    [timeLeftHours setStringValue:@"07"];}
                if ([timeLeftHours integerValue] == 6) {
                    [timeLeftHours setStringValue:@"06"];}
                if ([timeLeftHours integerValue] == 5) {
                    [timeLeftHours setStringValue:@"05"];}
                if ([timeLeftHours integerValue] == 4) {
                    [timeLeftHours setStringValue:@"04"];}
                if ([timeLeftHours integerValue] == 3) {
                    [timeLeftHours setStringValue:@"03"];}
                if ([timeLeftHours integerValue] == 2) {
                    [timeLeftHours setStringValue:@"02"];}
                if ([timeLeftHours integerValue] == 1) {
                    [timeLeftHours setStringValue:@"01"];}
                if ([timeLeftHours integerValue] == 0) {
                    [timeLeftHours setStringValue:@"00"]; }
                [timeLeftMins setIntegerValue: 59];
                [timeLeftSecs setIntegerValue: 59];
            }
        } else {
            [timeLeftMins setIntegerValue:([timeLeftMins integerValue] - 1)];
            if ([timeLeftMins integerValue] == 9) {
                [timeLeftMins setStringValue:@"09"];}
            if ([timeLeftMins integerValue] == 8) {
                [timeLeftMins setStringValue:@"08"]; }
            if ([timeLeftMins integerValue] == 7) {
                [timeLeftMins setStringValue:@"07"]; }
            if ([timeLeftMins integerValue] == 6) {
                [timeLeftMins setStringValue:@"06"]; }
            if ([timeLeftMins integerValue] == 5) {
                [timeLeftMins setStringValue:@"05"]; }
            if ([timeLeftMins integerValue] == 4) {
                [timeLeftMins setStringValue:@"04"];}
            if ([timeLeftMins integerValue] == 3) {
                [timeLeftMins setStringValue:@"03"]; }
            if ([timeLeftMins integerValue] == 2) {
                [timeLeftMins setStringValue:@"02"]; }
            if ([timeLeftMins integerValue] == 1) {
                [timeLeftMins setStringValue:@"01"]; }
            if ([timeLeftMins integerValue] == 0) {
                [timeLeftMins setStringValue:@"00"]; }
            [timeLeftSecs setIntegerValue: 59];
        }
    } else {
        [timeLeftSecs setIntegerValue:([timeLeftSecs integerValue] - 1)];
        if ([timeLeftSecs integerValue] == 9) {
            [timeLeftSecs setStringValue:@"09"]; }
        if ([timeLeftSecs integerValue] == 8) {
            [timeLeftSecs setStringValue:@"08"]; }
        if ([timeLeftSecs integerValue] == 7) {
            [timeLeftSecs setStringValue:@"07"]; }
        if ([timeLeftSecs integerValue] == 6) {
            [timeLeftSecs setStringValue:@"06"]; }
        if ([timeLeftSecs integerValue] == 5) {
            [timeLeftSecs setStringValue:@"05"]; }
        if ([timeLeftSecs integerValue] == 4) {
            [timeLeftSecs setStringValue:@"04"]; }
        if ([timeLeftSecs integerValue] == 3) {
            [timeLeftSecs setStringValue:@"03"]; }
        if ([timeLeftSecs integerValue] == 2) {
            [timeLeftSecs setStringValue:@"02"]; }
        if ([timeLeftSecs integerValue] == 1) {
            [timeLeftSecs setStringValue:@"01"]; }
        if ([timeLeftSecs integerValue] == 0) {
            [timeLeftSecs setStringValue:@"00"]; }
        
    }
}

@end
